package com.library.api.service.impl;

import com.library.api.service.IHomeApiService;
import com.library.domain.AppActivity;
import com.library.domain.AppBanner;
import com.library.domain.BoBook;
import com.library.domain.HotConfig;
import com.library.enums.HotTypeEnum;
import com.library.service.IAppActivityService;
import com.library.service.IAppBannerService;
import com.library.service.IBoBookService;
import com.library.service.IHotConfigService;
import com.library.vo.AppActivityModel;
import com.library.vo.HomeModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author julyWhj
 * @Description $
 * @Date 2022/3/28 10:56 上午
 **/
@Service
public class HomeApiServiceImpl implements IHomeApiService {
    @Autowired
    private IAppBannerService appBannerService;
    @Autowired
    private IAppActivityService activityService;
    @Autowired
    private IBoBookService boBookService;
    @Autowired
    private IHotConfigService hotConfigService;

    @Override
    public HomeModel home() {
        HomeModel homeModel = new HomeModel();
        AppBanner appBannerReq = new AppBanner();
        List<AppBanner> appBanners = appBannerService.selectAppBannerList(appBannerReq);
        homeModel.setAppBanners(appBanners);
        List<HotConfig> hotConfigs = hotConfigService.selectHotConfigList(new HotConfig());
        List<BoBook> hotBooks = new ArrayList<>();
        List<AppActivityModel> hotActivityList = new ArrayList<>();
        hotConfigs.forEach(hotConfig -> {
            if (hotConfig.getHotType() == HotTypeEnum.BOOK.getType()) {
                BoBook boBook = boBookService.selectBoBookById(Long.valueOf(hotConfig.getHotId()));
                hotBooks.add(boBook);
            } else if (hotConfig.getHotType() == HotTypeEnum.ACTIVITY.getType()) {
                AppActivity appActivity = activityService.selectAppActivityByActivityId(Long.valueOf(hotConfig.getHotId()));
                hotActivityList.add(AppActivityModel.toModel(appActivity));
            }
        });
        homeModel.setAppActivities(hotActivityList);
        homeModel.setBoBooks(hotBooks);
        return homeModel;
    }
}
